package com.cloudcc.mobile.view.schedule;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.log.Log;
import com.mypage.adapter.NewListAdapter;
import com.mypage.bean.NewTaskBean;
import com.mypage.bean.NewTaskBean.mDlist;
import com.mypage.utils.UtilsShowDialog;
import com.mypage.view.custom.XListView;

import java.util.ArrayList;
import java.util.List;

public class NewListActivity extends Activity implements
		XListView.IXListViewListener,TextView.OnEditorActionListener {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private XListView mListView;
	private final List mList = new ArrayList<>();
	private Handler mHandler;
	private int start = 1;
	private static int refreshCnt = 0;
	public TextView bar_title_text;
	public Button mBtnBack;
	public String mID;
	public String mName;
	public NewTaskBean mDdata;
	public NewListAdapter mAdapter;
	private EditText mSearch;
	private String mSearchData = "";
	private UtilsShowDialog showDialog;

	private TextView zanwu_more;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newist);
		showDialog = new UtilsShowDialog(this);
		mSearch = (EditText) findViewById(R.id.mSearch);
		mSearch.setOnEditorActionListener(this);
		bar_title_text = (TextView) findViewById(R.id.bar_title_text);


		zanwu_more = (TextView)findViewById(R.id.zanwu_more);
		mBtnBack = (Button) findViewById(R.id.mBtnBack);// 返回按钮
		mID = getIntent().getStringExtra("ID");
		mName = getIntent().getStringExtra("Name");

		mListView = (XListView) findViewById(R.id.xListView);

		// mListView.setPullLoadEnable(true);
		// mAdapter = new ArrayAdapter<String>(this, R.layout.xxlist_item,
		// items);
		// mListView.setAdapter(mAdapter);
		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener(this);
		mHandler = new Handler();

		if ("en".equals(mEns)) {
			bar_title_text.setText("List values");
		}else{
			bar_title_text.setText(mName);
		}
if ("en".equals(mEns)) {
	showDialog.onShowLoadingDialog("loading...");
		}else{
			showDialog.onShowLoadingDialog("正在加载...");
		}

	
		setHttp();
		initListener();
	}

	public void initListener() {
		mBtnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
if ("en".equals(mEns)) {
	mListView.setRefreshTime(" just");
		}else{
			mListView.setRefreshTime(" 刚刚");
		}

		
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				start = 1;
				setHttp();
				onLoad();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {

		start++;
		setHttp_s();

		onLoad();
	}

	/**
	 * 访问网络
	 */
	public void setHttp() {
		
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());
		params.addBodyParameter("serviceName", "getLookupValue");
		params.addBodyParameter("objectId", mID);
		params.addBodyParameter("pageSize", String.valueOf(start));
		params.addBodyParameter("keyword", mSearchData);
		Log.d("列表值url-------------------------", UrlManager.getInterfaceUrl()
				+ "?binding=" + RunTimeManager.getInstance().getServerBinding()
				+ "&serviceName=getLookupValue"
				+ "&objectId=2016360343BD7B2SQ2PO" + "&pageSize=1"
				+ "&keyword=");
		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						
						showDialog.onDismissLoadingDialog();
						Log.d("列表值接口失败-------------------------", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						showDialog.onDismissLoadingDialog();
						Log.d("列表值接口成功-------------------------", arg0.result);
						NewTaskBean mDdata = new Gson().fromJson(arg0.result,
								NewTaskBean.class);
						mList.clear();

						if(mDdata.data.size()<=0) {
							mListView.setVisibility(View.GONE);
							zanwu_more.setVisibility(View.VISIBLE);
						}else {
							mList.addAll(mDdata.data);
							mListView.setVisibility(View.VISIBLE);
							zanwu_more.setVisibility(View.GONE);
								mAdapter = new NewListAdapter(NewListActivity.this, mList);
								mListView.setAdapter(mAdapter);
//								mAdapter.notifyDataSetChanged();

						}

						setListenerItem(mDdata.data);
					}

				});

	}

	/**
	 * 访问网络加载更多
	 */
	public void setHttp_s() {

		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());
		params.addBodyParameter("serviceName", "getLookupValue");
		params.addBodyParameter("objectId", mID);
		params.addBodyParameter("pageSize", String.valueOf(start));
		params.addBodyParameter("keyword", "");
		Log.d("列表值url-------------------------", UrlManager.getInterfaceUrl()
				+ "?binding=" + RunTimeManager.getInstance().getServerBinding()
				+ "&serviceName=getLookupValue"
				+ "&objectId=2016360343BD7B2SQ2PO" + "&pageSize=1"
				+ "&keyword=");
		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.d("列表值接口失败-------------------------", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.d("列表值接口成功-------------------------", arg0.result);
						NewTaskBean mDdata = new Gson().fromJson(arg0.result,
								NewTaskBean.class);

						mList.addAll(mDdata.data);
						mAdapter.notifyDataSetChanged();
						setListenerItem(mList);
					}

				});

	}

	public void setListenerItem(final List<mDlist> data) {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent mIntent = new Intent();
				if (data.size()<=0) {
					return;
				}
				Log.d("9999-------------", "id   ="+data.get(position-1).id+"     name="+data.get(position-1).name);
				String ids = data.get(position-1).id;
				String names = data.get(position-1).name;
//				NewRelatedActivity.instance.finish();
				mIntent.putExtra("AVname", mName+"-"+names);
				mIntent.putExtra("AV", ids);
				setResult(1, mIntent);
				finish();
			}
		});
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (actionId == EditorInfo.IME_ACTION_SEARCH) {
			mSearchData = mSearch.getText().toString().trim();
			setHttp();
			// 当按了搜索之后关闭软键盘
			((InputMethodManager) mSearch.getContext().getSystemService(
					Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this
					.getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
			return true;
		}
		return false;
	}


}